﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="TomCorp">
//   Thomas Heuberger
// </copyright>
// <summary>
//   Interaction logic for MainWindow.xaml
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace FHIntranetParser {
    using System;
    using System.Diagnostics.CodeAnalysis;
    using System.Threading.Tasks;
    using System.Windows;

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly", Justification = "Reviewed. Suppression is OK here.")]
    public partial class MainWindow : Window {
        /// <summary>
        /// Initializes a new instance of the <see cref="MainWindow"/> class.
        /// </summary>
        [SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1126:PrefixCallsCorrectly", Justification = "Reviewed. Suppression is OK here.")]
        public MainWindow() {
            InitializeComponent();

            this.HistoryFilePathTextBox.Text = System.IO.Path.GetTempPath() + "\\FHHistory.xml";
        }

        /// <summary>
        /// Creates a log entry.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        private static string CreateLogEntry(string message) {
            return string.Format("[{0}]: {1}{2}", DateTime.Now.ToLongTimeString(), message, Environment.NewLine);
        }

        /// <summary>
        /// The button_ click.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button_Click(object sender, RoutedEventArgs e) {
            this.GradesParser.HistoryFilePath = HistoryFilePathTextBox.Text;
            this.GradesParser.ConfigFilePath = ConfigFilePathTextBox.Text;
            this.GradesParser.StartParsing(new TimeSpan(0, (int)this.IntervalSlider.Value, 0));
        }

        /// <summary>
        /// Event handler for the GradesParser Reporting event.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void GradesParser_Reporting(object sender, MessageEventArgs e) {
            switch (e.Message) {
                case "StudentInfo":
                    this.StudentTextBox.Text = e.Info.Item1;
                    this.MatrNumberTextBox.Text = e.Info.Item2;
                    break;
                case "InvalidCredentials":
                    MessageBox.Show("Invalid Credentials! Go fuck yourself!");
                    break;
                case "NewGrades":
                    const string Message = "New grades found!";
                    
                    Task.Run(() => {
                        MessageBox.Show(Message);
                    });

                    this.LogTextBox.Text += CreateLogEntry(Message);
                    break;
                case "FetchedGrades":
                    this.LogTextBox.Text += CreateLogEntry("Fetched " + e.Info.Item1 + " grades from server!");
                    this.AvgTextBox.Text = e.Info.Item2;
                    this.EctsTextBox.Text = e.Info.Item3;
                    break;
                default:
                    this.LogTextBox.Text += CreateLogEntry(e.Message);
                    break;
            }
        }

        /// <summary>
        /// The button_ click_1.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button_Click_1(object sender, RoutedEventArgs e) {
            var openFileDialog = new System.Windows.Forms.OpenFileDialog {
                DefaultExt = ".xml",
                Filter = "Extensible Markup Language (.xml)|*.xml"
            };

            var result = openFileDialog.ShowDialog();

            if (result != System.Windows.Forms.DialogResult.OK) return;

            ConfigFilePathTextBox.Text = openFileDialog.FileName;
        }

        /// <summary>
        /// The button_ click_2.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void Button_Click_2(object sender, RoutedEventArgs e) {         
            var folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
            var result = folderBrowserDialog.ShowDialog();

            if (result != System.Windows.Forms.DialogResult.OK) return;

            HistoryFilePathTextBox.Text = folderBrowserDialog.SelectedPath + "\\FHHistory.xml";
        }
    }
}
